package com.kudian.restful.controller;

import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.PropertiesLoader;
import com.kudian.restful.service.AdService;
import com.kudian.restful.service.GoodsTypeService;
import com.kudian.restful.service.LoginSessionService;
import com.kudian.restful.service.SmsService;
import com.kudian.restful.vo.ad.CarouselRetVO;
import com.kudian.restful.vo.ad.MidAdRetVO;
import com.kudian.restful.vo.ad.MidAdVO;
import com.kudian.restful.vo.sms.SendVeriVO;
import com.kudian.restful.vo.sms.VeriVO;
import com.kudian.restful.vo.welinfo.WelInfoPageRetVO;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * Created by Administrator on 2016/4/27.
 */
@RestController
@RequestMapping("sms")
public class SMSController {

    private static final Logger logger = Logger.getLogger(SMSController.class);

    @Autowired
    private SmsService smsService;

    @RequestMapping(value = "sendVerificationCd", method = RequestMethod.POST)
    @ApiOperation(value = "发送验证码", httpMethod = "POST", notes = "发送验证码", response = ResultObj.class)
    public @ResponseBody ResultObj sendVerificationCd(@RequestBody SendVeriVO vo) {
        ResultObj o = new ResultObj();
        try {
            this.smsService.sendVerificationCd(vo);
            // 失败
            o.setErrcode(0);
            o.setErrmsg("成功发送验证码!");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("发送验证码失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "checkVerificationCd", method = RequestMethod.POST)
    @ApiOperation(value = "校验验证码是否有效", httpMethod = "POST", notes = "校验验证码是否有效(十分钟内)", response = ResultObj.class)
    public @ResponseBody ResultObj checkVerificationCd(@RequestBody VeriVO vo) {
        ResultObj o = new ResultObj();
        try {
            if (smsService.checkVerificationCd(vo)) {
                // 失败
                o.setErrcode(0);
                o.setErrmsg("验证码有效!");
            } else {
                // 失败
                o.setErrcode(9999);
                o.setErrmsg("验证码无效!");
            }
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("验证码无效!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    public static void main(String[] args) {

        PropertiesLoader p = new PropertiesLoader("env.properties");
        String url = p.getProperty("SMS_URL");
        String appkey = p.getProperty("SMS_APP_KEY");
        String secret = p.getProperty("SMS_APP_SECRET");

        Random rnd = new Random();
        int num = rnd.nextInt(89999) + 10000;

        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend( "" );
        req.setSmsType( "normal" );
        req.setSmsFreeSignName( "注册验证" );
        req.setSmsParamString( "{code:'" + num +  "',product:'先客'}" );
        req.setRecNum( "13821286530" );
        req.setSmsTemplateCode( "SMS_12460462" );
        AlibabaAliqinFcSmsNumSendResponse rsp = null;

        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }
}
